package com.yanyeori.activiti.controller;

import com.yanyeori.framework.core.util.JacksonUtil;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chenkuan
 * @since 2022/5/27 027 14:41
 */
@RestController
@RequestMapping("/activiti")
public class ActivitiController {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessRuntime processRuntime;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    /**
     * 启动请假流程
     **/
    @PostMapping("/start")
    public void start(String instanceKey) {
        System.out.println("开启请假流程...");
        Map<String, Object> map = new HashMap<>();
        //在holiday.bpmn中,填写请假单的任务办理人为动态传入的userId,此处模拟一个id
        map.put("userId", "10001");

        //部署新流程
//        Deployment deployment = repositoryService.createDeployment()
//                .addClasspathResource("processes/Process_1.bpmn")
//                .name("请假流程")
//                .deploy();
//        String deploymentId = deployment.getId();

        //开启流程实例
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(instanceKey, map);

        System.out.println("启动流程实例成功: " + instance);
        System.out.println("流程实例ID: " + instance.getId());
        System.out.println("流程定义ID: " + instance.getProcessDefinitionId());
    }

    /**
     * 员工申请
     **/
    @PostMapping("/employeeApply")
    public void employeeApply(String instanceId) {
        String leaveDays = "18"; // 请假天数
        String leaveReason = "回家结婚"; // 请假原因
        Task task = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
        if (task == null) {
            System.out.println("任务ID: " + instanceId + " 查询到任务为空！");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("days", leaveDays);
        map.put("date", new Date());
        map.put("reason", leaveReason);
        taskService.complete(task.getId(), map);
        System.out.println("执行【员工申请】环节，流程推动到【上级审核】环节");
    }

    /**
     * 领导审批
     **/
    @PostMapping("/departmentAudit")
    public void departmentAudit(String instanceId) {
        String departmentalOpinion = "恭喜恭喜，早生贵子";
        Task task = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
        Map<String, Object> map = new HashMap<>();
        map.put("departmentalOpinion", departmentalOpinion);
        taskService.complete(task.getId(), map);
        System.out.println("添加审批意见,请假流程结束");
    }

    /**
     * 查看请假信息
     **/
    @PostMapping("/showTaskInfo")
    public void showTaskInfo(String instanceId) {
        Task task = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
        Map<String, Object> variables = taskService.getVariables(task.getId());
        String days = taskService.getVariable(task.getId(), "days", String.class);
        Date date = taskService.getVariable(task.getId(), "date", Date.class);
        String reason = taskService.getVariable(task.getId(), "reason", String.class);
        String userId = taskService.getVariable(task.getId(), "userId", String.class);
        System.out.println("所有参数:  " + JacksonUtil.toJson(variables));
        System.out.println("请假天数:  " + days);
        System.out.println("请假理由:  " + reason);
        System.out.println("请假人id:  " + userId);
        System.out.println("请假日期:  " + date.toString());
    }
}
